/**
 * This program is free software but WITHOUT ANY WARRANTY OR CONDITIONS OF ANY KIND.
 *
 * Copyright (C) 2008-2013 www.pretty-tools.com
 */

document.title = chrome.i18n.getMessage("webslideshow_dlg_title");

var resourceURL = null;
try
{
    resourceURL = chrome.extension.getURL("images/");
}
catch(ex) {}

function closeWindow()
{
    yrprxBrowser.closeWindow();
    onUnload(); // we need to call it implicit
}

function onLoad(event)
{
    var wnd = document.getElementById("wsshSlideShowDialog");
    if (wnd) wnd.setAttribute("full_screen_title", chrome.i18n.getMessage("webslideshow_dlg_full_screen_title"));

    yrprxHTMLContentHelper.enumerateElementsByTagName(document, "img", function(img) {
        setDefaultImage(img);
        // load tooltips for images
        var tooltip = img.getAttribute("title");
        if (tooltip) img.setAttribute("title", chrome.i18n.getMessage(tooltip));
        var tooltip_format = img.getAttribute("tooltip_format");
        if (tooltip_format) img.setAttribute("tooltip_format", chrome.i18n.getMessage(tooltip_format));
    });
    var panel = document.getElementById("slideshow_panel_screen");
    if (panel)
    {
        panel.style.visibility = "visible";
        setPosition(panel, (screen.width-panel.clientWidth)/2, (screen.height-panel.clientHeight));

        panel.onmouseover = function (event) {
            gSlideShow.onMouseOverPanel(event);
        };
        panel.onmouseout = function (event) {
            gSlideShow.onMouseOutPanel(event);
        };
        panel.ondblclick = function (event) {
            window.event.cancelBubble = true;
        };
    }
    var btnSave = document.getElementById("save");
    if (btnSave) btnSave.style.display = "none"; //TODO: currently we don't support save for Google Chrome

    if (window.arguments.length > 0)
        gSlideShow.onLoad(event);

    window.pageLoaded = true;

    var body = document.body;
    body.onkeydown = onKeyPress;
    body.onunload = onUnload;
    body.oncontextmenu = onContextMenu;
    body.ondblclick = closeWindow;
    body.onselectstart = function() {
        return false
    };

    var buttonsIds = ["save", "zoom_on", "zoom_off", "shrink_on", "shrink_off", "repeat_on", "repeat_off", "minus", "plus", "first", "previous", "pause", "play", "next", "last"];
    for (var i in buttonsIds)
    {
        var buttonsId = buttonsIds[i];
        var button = document.getElementById(buttonsId);
        if (!button) continue;

        var default_image = button.getAttribute("default_image");
        if (!default_image) continue;

        var idx = default_image.indexOf('.png');
        if (idx == -1) continue;

        button.setAttribute("highlight_image", default_image.substring(0, idx) + "_highlight" + default_image.substring(idx));
        button.setAttribute("pushed_image", default_image.substring(0, idx) + "_pushed" + default_image.substring(idx));

        button.isButtonEnabled = function () {
            try {
                var click_if = this.getAttribute("click_if");
                return !click_if ? true : gSlideShow[click_if]();
            } catch (e) {
            }
        };

        button.onclick = function () {
            try
            {
                if (!this.isButtonEnabled())
                    return;

                var on_click = this.getAttribute("on_click");
                if (!on_click) return;

                if ("next" == this.id)
                    gSlideShow[on_click](true);
                else
                    gSlideShow[on_click]();
            }
            catch (e)
            {
            }
        };

        button.onmousedown = function () {
            if (this.isButtonEnabled())
                setPushedImage(this);
        };

        button.onmouseover = function () {
            if (this.isButtonEnabled())
                setHighlightImage(this);
        };

        button.onmouseup = function () {
            if (this.isButtonEnabled())
                setHighlightImage(this);
            else
                setDefaultImage(this);
        };

        button.onmouseout = function () {
            setDefaultImage(this);
        };
    }

}

function onUnload()
{
    gSlideShow.onUnLoad();

    var port = chrome.extension.connect({name: "clearTheLastSlideShowDialog"});
    port.postMessage({clearTheLastSlideShowDialog: window.thisWnd});
}

function onKeyPress(event)
{
    if (event.keyCode == yrprxKeyboardHelper.VK_F11 || event.keyCode == 83 && event.ctrlKey /* Ctrl+S */) {
        return true;
    }

    gSlideShow.onKeyPress(event);
    return false;
}

function changeImage(img, src)
{
    if (img && src)
        img.src = resourceURL + src;
}

function setDefaultImage(img)
{
    changeImage(img, img.getAttribute("default_image"));
}

function setPushedImage(img)
{
    changeImage(img, img.getAttribute("pushed_image"));
}

function setHighlightImage(img)
{
    changeImage(img, img.getAttribute("highlight_image"));
}

function setDefaultImageFor(imageId)
{
    var img = document.getElementById(imageId);
    if (img) setDefaultImage(img);
}

function swapImages(hideImageId,showImageId)
{
    var hideImage = document.getElementById(hideImageId);
    if (hideImage) hideImage.style.display = "none";
    var showImage = document.getElementById(showImageId);
    if (showImage)
    {
        setDefaultImage(showImage);
        showImage.style.display = "inline";
    }
}

function createImage(idx)
{
    var image = document.createElement("image");
    // set file index
    image.setAttribute("id", "image" + idx);
    image.setAttribute("idx", idx);
    image.style.visibility = 'hidden';
    image.style.position = 'absolute';
    image.style.left = '0px';
    image.style.top = '0px';
    image.style.zIndex = '1';

    return image;
}

function setPosition(elem, left, top)
{
    if (elem)
    {
        elem.style.left = left;
        elem.style.top = top;
    }
}

function setPositionLeft(elem, left)
{
    if (elem) elem.style.left = left + 'px';
}

function setPositionTop(elem, top)
{
    if (elem) elem.style.top = top + 'px';
}

function setImageSize(img, size)
{
    img.style.width = size.width + "px";
    img.style.height = size.height + "px";
}

function setTooltip(elem, tooltip)
{
    if (elem) elem.setAttribute("title", tooltip);
}

function onContextMenu()
{
    // Allow context menu for images
    try
    {
        var srcElement = window.event.srcElement;

        return ("IMG" == srcElement.tagName) && srcElement.getAttribute("idx") != null;
    }
    catch(ex) {}
    return false;
}

function showControlPanel()
{
    try
    {
        var panel = document.getElementById("slideshow_panel");

        if (panel && panel.style.display == "none")
            panel.style.display = "";
    }
    catch(ex)
    {
        if (gSlideShow.logger.enabled) gSlideShow.logger.error("showControlPanel: ", ex);
    }
}

function hideControlPanel()
{
    try
    {
        var panel = document.getElementById("slideshow_panel");
        if (panel) panel.style.display = "none";
    }
    catch(ex)
    {
        if (gSlideShow.logger.enabled) gSlideShow.logger.error("hideControlPanel: ", ex);
    }
}

addEventListener("load", onLoad);
